#ifndef _DYNAMIC_SEQ_LIST_H_
#define _DYNAMIC_SEQ_LIST_H_

#include "SeqList.h"
#include "Exception.h"

namespace MySTL
{

template < typename T>
class DynamicSeqList : public SeqList<T>
{
public:
	DynamicSeqList(int capacity);
	~DynamicSeqList();
	void resize(int capacity);
	int capacity() const;

protected:
	int m_capacity;
};

template < typename T>
DynamicSeqList<T>::DynamicSeqList(int capacity)
{
	m_capacity = 0;

	if (capacity <= 0)
	{
		THROW_EXCEPTION(ParameterErrorException, "Param capacity is invalid in DynamicSeqList<T>::DynamicSeqList(int capacity)");
	}

	this->m_array = new T[capacity];

	if (NULL != this->m_array)
	{
		m_capacity = capacity;
	}
	else
	{
		THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to construct DynamicSeqList in DynamicSeqList<T>::DynamicSeqList(int capacity)");
	}
}

template < typename T>
DynamicSeqList<T>::~DynamicSeqList()
{
	delete[] this->m_array;
	this->m_array = NULL;
}

template < typename T>
void DynamicSeqList<T>::resize(int capacity)
{
	if (capacity <= 0)
	{
		THROW_EXCEPTION(ParameterErrorException, "Param capacity is invalid in void DynamicSeqList<T>::resize(int capacity)");
	}

	if (capacity != m_capacity)
	{
		T* space = new T[capacity];
		if (NULL != space)
		{
			int len = (capacity < this->m_length) ? capacity : this->m_length;
			for (int i = 0; i < len; ++i)
			{
				space[i] = this->m_array[i];
			}

			T* toDel = this->m_array;

			this->m_array = space;
			this->m_length = len;
			m_capacity = capacity;

			delete[] toDel;
		}
		else
		{
			THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to resize DynamicSeqList in void DynamicSeqList<T>::resize(int capacity)");
		}
	}
}

template < typename T>
int DynamicSeqList<T>::capacity() const
{
	return m_capacity;
}

}

#endif